package com.fyzk.view;

import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;

public class LimitEditText extends AppCompatEditText {

    private static final String TAG = "DJ_EditText";

    private Context context;
    private EditText et;
    private int max = 0;
    private String tiShi = "";
    private LimitEditTextChangeListener listener;

    public LimitEditText(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public LimitEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    public LimitEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        initView();
    }

    private void initView() {
        this.et = this;
        et.addTextChangedListener(new DJEditText_TextWatcher());
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getMax() {
        return this.max;
    }

    //接口
    public interface LimitEditTextChangeListener {
        void changText();
    }

    public void setEditTextChangeListener(LimitEditTextChangeListener listener) {
        this.listener = listener;
    }


    class DJEditText_TextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int arg1, int arg2,
                                      int arg3) {
            //s:变化前的所有字符； start:字符开始的位置； count:变化前的总字节数；after:变化后的字节数
        }

        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2,
                                  int arg3) {
            //S：变化后的所有字符；start：字符起始的位置；before: 变化之前的总字节数；count:变化后的字节数
            String maxString = max + "";
            int afterNum = 0;
            try {
                afterNum = Integer.parseInt(s.toString());
            } catch (Exception e) {

            }
            if (s.length() > maxString.length() || afterNum > max) {
//            	Toast.makeText(context, ""+tiShi, 2000).show();
                et.setText(maxString);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            //s:变化后的所有字符
            String afterStr = s + "";
            if (afterStr.length() == 2) {
                String str = afterStr.charAt(0) + "";
                if (str.equals("0")) {
                    afterStr = afterStr.substring(1, afterStr.length());
                    et.setText(afterStr);
                }
            }
            et.setSelection(et.length());

            if (null != listener) {
                listener.changText();
            }
        }

    }

    public String getTiShi() {
        return tiShi;
    }

    public void setTiShi(String tiShi) {
        this.tiShi = tiShi;
    }

    public int getTextNum() {
        int num = 0;
        try {
            num = Integer.parseInt(et.getText().toString());
        } catch (Exception e) {
            // TODO: handle exception
        }

        return num;
    }

}
